package com.aps.apspda.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.aps.apspda.R;
import com.aps.apspda.dialog.GeneralPop;
import com.aps.apspda.entity.InputDataEntity;
import com.aps.apspda.utils.DisplayUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author
 * @date 2017/9/11
 * @desc
 */

public class InputDataListAdapter extends BaseQuickAdapter<InputDataEntity.InputObject, BaseViewHolder> {
    private boolean isSetValue = false;

    public InputDataListAdapter(@Nullable List<InputDataEntity.InputObject> data) {
        super(R.layout.adapter_input_list, data);
    }

    public void setIsSetValue(boolean isSetValue) {
        this.isSetValue = isSetValue;
    }

    @Override
    protected void convert(BaseViewHolder helper, final InputDataEntity.InputObject item) {
        TextView textView = helper.getView(R.id.tvTitle);
        if (item.getISNULL().equalsIgnoreCase("Y")) {
            helper.setText(R.id.tvTitle, "*" + item.getDATACNNAME());
            textView.getPaint().setFakeBoldText(true);//加粗
        } else {
            helper.setText(R.id.tvTitle, item.getDATACNNAME());
            textView.getPaint().setFakeBoldText(false);//加粗
        }
        EditText editText = helper.getView(R.id.etContent);
        LinearLayout llCheck = helper.getView(R.id.llCheck);
        final TextView tvChoose = helper.getView(R.id.tvChoose);
        LinearLayout llChoose = helper.getView(R.id.llChoose);
        final EditText etChooseOther = helper.getView(R.id.etChooseOther);
        final EditText etNGChooseOther = helper.getView(R.id.etNGChooseOther);

        LayoutInflater inflater = LayoutInflater.from(mContext);
        final View popView = inflater.inflate(R.layout.popupwindow_down_list_select, null);
        RecyclerView lvDownList = popView.findViewById(R.id.lvDownList);
        lvDownList.setLayoutManager(new LinearLayoutManager(mContext));
        lvDownList.setItemAnimator(new DefaultItemAnimator());
        final PopDownAreaListAdapter popDownAreaListAdapter = new PopDownAreaListAdapter(new ArrayList<String>());
        popDownAreaListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        popDownAreaListAdapter.isFirstOnly(false);
        popDownAreaListAdapter.setCurrentSelect(0);
        lvDownList.setAdapter(popDownAreaListAdapter);
        //给选项框赋
        tvChoose.setText(item.getSELECTVALUE());
        switch (item.getDATATYPE()) {
            case "1":
                if (isSetValue) {
                    editText.setText(item.getDATAVALUE());
                }
                editText.setVisibility(View.VISIBLE);
                llCheck.setVisibility(View.GONE);
                llChoose.setVisibility(View.GONE);
                if (item.getLANGUAGETYPE().equals("A")) {
                    editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                } else {
                    editText.setInputType(InputType.TYPE_CLASS_TEXT);
                }
                setEditListener(editText, item);
                //显示输入框
                showEditText(item, etChooseOther);
                break;
            case "2":
                editText.setVisibility(View.GONE);
                llCheck.setVisibility(View.VISIBLE);
                llChoose.setVisibility(View.GONE);
                if (item.getLANGUAGETYPE().equals("A")) {
                    etNGChooseOther.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                } else {
                    etNGChooseOther.setInputType(InputType.TYPE_CLASS_TEXT);
                }
                final RadioButton rbOK = helper.getView(R.id.rbOK);
                final RadioButton rbNG = helper.getView(R.id.rbNG);
                final RadioGroup rbGroup = helper.getView(R.id.rbGroup);
                final TextView tvNGChoose = helper.getView(R.id.tvNGChoose);
                setEditListener(etNGChooseOther, item);
                //给选项框赋
                if (item.getSELECTVALUE().equals("")) {
                    tvNGChoose.setText("请选择");
                } else {
                    tvNGChoose.setText(item.getSELECTVALUE());
                }
                Log.e("---" + item.getSELECTVALUE(), "---" + item.getDATAVALUE());
                rbGroup.clearCheck();
                if (item.getDATAVALUE().equals("")) {
                    rbNG.setChecked(false);
                    rbOK.setChecked(false);
                    tvNGChoose.setVisibility(View.GONE);
                    etNGChooseOther.setVisibility(View.GONE);
                } else {
                    //显示Radio
                    showRadio(item, rbNG, rbOK);
                    //显示选项框
                    showSelectBox(item, tvNGChoose);
                    //显示输入框
                    showEditText(item, etNGChooseOther);
                }

                tvNGChoose.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        final GeneralPop titlePop = new GeneralPop(mContext, popView
//                                , tvNGChoose.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
                        final GeneralPop titlePop = new GeneralPop(mContext, popView
                                , tvNGChoose.getWidth(), DisplayUtil.dip2px(150));//modify by bunny
                        String otherStr1 = item.getDATATYPEDETAIL() + ";其他";
                        String[] strings1 = otherStr1.split(";");
                        final List<String> list1 = Arrays.asList(strings1);
                        popDownAreaListAdapter.setNewData(list1);
                        popDownAreaListAdapter.setOnItemClickListener(new OnItemClickListener() {
                            @Override
                            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                popDownAreaListAdapter.setCurrentSelect(position);
                                popDownAreaListAdapter.notifyDataSetChanged();
                                tvNGChoose.setText(list1.get(position));
                                item.setSELECTVALUE(list1.get(position));//将选项框的值填到集合中
                                if (list1.get(position).equals("其他")) {
                                    etNGChooseOther.setVisibility(View.VISIBLE);
                                } else {
                                    etNGChooseOther.setVisibility(View.GONE);
                                    item.setDATAVALUE("NG;" + list1.get(position));
                                }
                                titlePop.dismiss();
                            }
                        });
                        titlePop.showAsDropDown(tvNGChoose, 0, 0);
                    }
                });
                rbOK.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        item.setDATAVALUE("OK");
                     /*   etNGChooseOther.setVisibility(View.GONE);
                        tvNGChoose.setVisibility(View.GONE);*/
                        /*2020-1-5 修改  Start*/
                        if (tvNGChoose.getText().toString().equals("其他")) {
                            etNGChooseOther.setVisibility(View.VISIBLE);
                        } else {
                            etNGChooseOther.setVisibility(View.GONE);
                        }
                        tvNGChoose.setVisibility(View.VISIBLE);
                        /*2020-1-5 修改  End */
                    }
                });
                rbNG.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        item.setDATAVALUE("NG;");
                        if (tvNGChoose.getText().toString().equals("其他")) {
                            etNGChooseOther.setVisibility(View.VISIBLE);
                        } else {
                            etNGChooseOther.setVisibility(View.GONE);
                        }
                        tvNGChoose.setVisibility(View.VISIBLE);
                    }
                });

                break;
            case "3":
                editText.setVisibility(View.GONE);
                llCheck.setVisibility(View.GONE);
                llChoose.setVisibility(View.VISIBLE);
                if (item.getLANGUAGETYPE().equals("A")) {
                    etChooseOther.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                } else {
                    etChooseOther.setInputType(InputType.TYPE_CLASS_TEXT);
                }
                setEditListener(etChooseOther, item);
                if (item.getDATAVALUE().equals("")) {
                    tvChoose.setVisibility(View.GONE);
                    etChooseOther.setVisibility(View.GONE);
                } else {
                    //显示选项框
                    showSelectBox(item, tvChoose);
                    //显示输入框
                    showEditText(item, etChooseOther);

                }
                tvChoose.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final GeneralPop titlePop2 = new GeneralPop(mContext, popView
                                , tvChoose.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
                        String otherStr = item.getDATADETAIL() + ";其他";
                        String[] strings = otherStr.split(";");
                        final List<String> list = Arrays.asList(strings);
                        popDownAreaListAdapter.setNewData(list);
                        popDownAreaListAdapter.setOnItemClickListener(new OnItemClickListener() {
                            @Override
                            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                popDownAreaListAdapter.setCurrentSelect(position);
                                popDownAreaListAdapter.notifyDataSetChanged();
                                tvChoose.setText(list.get(position));
                                item.setSELECTVALUE(list.get(position));//将选项框的值填到集合中
                                if (list.get(position).equals("其他")) {
                                    etChooseOther.setVisibility(View.VISIBLE);
                                } else {
                                    etChooseOther.setVisibility(View.GONE);
                                    item.setDATAVALUE(list.get(position));
                                }
                                titlePop2.dismiss();
                            }
                        });
                        titlePop2.showAsDropDown(tvChoose, 0, 0);
                    }
                });
                break;
        }
    }

    /**
     * 显示输入框的变化情况
     *
     * @param item
     * @param etChooseOther
     */
    private void showEditText(InputDataEntity.InputObject item, EditText etChooseOther) {
        //如果选项框的值为其他，则显示输入框，否则不显示
        if (item.getSELECTVALUE().equals("其他")) {
            etChooseOther.setVisibility(View.VISIBLE);
            //Recycler多个条目混淆，会导致EdtiText的值混乱，将值保存下来，根据条目重新给EditText赋值。
             etChooseOther.setText(item.getINPUTVALUE());
        } else {
            etChooseOther.setVisibility(View.GONE);
        }
    }

    /**
     * 显示选项框的变化情况
     *
     * @param item
     * @param tvChoose
     */
    private void showSelectBox(InputDataEntity.InputObject item, TextView tvChoose) {
        // 如果OK和NG有一个选中，则显示下拉选项框
        if (item.getDATAVALUE().contains("OK") || item.getDATAVALUE().contains("NG")) {
            tvChoose.setVisibility(View.VISIBLE);
        } else {
            tvChoose.setVisibility(View.GONE);
        }
    }

    /**
     * 显示RadioButton的变化情况
     *
     * @param item
     * @param rbNG
     * @param rbOK
     */
    private void showRadio(InputDataEntity.InputObject item, RadioButton rbNG, RadioButton rbOK) {
        //判断是否包含NG和OK字段 ，
        if (item.getDATAVALUE().contains("NG")) {
            rbNG.setChecked(true);
        } else {
            rbNG.setChecked(false);
        }
        if (item.getDATAVALUE().contains("OK")) {
            rbOK.setChecked(true);
        } else {
            rbOK.setChecked(false);
        }
    }

    private void setEditListener(EditText editText, final InputDataEntity.InputObject item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }
        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setINPUTVALUE(s.toString());
                if (item.getDATAVALUE().toUpperCase().contains("NG")) {
                    item.setDATAVALUE(s.length() > 0 ? "NG;" + s.toString() : "NG;");
                } else {
                    item.setDATAVALUE(s.length() > 0 ? "OK;" + s.toString() : "OK;");

                }
            }
        };
        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
